'use strict';
/**
 * 角色权限管理Service
 */
app.service('UserService', 
['$http', '$q', 'dialogs', 
function ($http, $q, dialogs) {
	var that = this;
	this.getGridOptions = function(){
		var _gridWidth = 1024;
        var _columnWidth = _gridWidth / 6;
		return {
			//height: getGridHeight(),
			url: ISS.getPath('/user/userlist'),
			dataType: 'json',
			colModel : [
				{display : 'ID',name : 'id',width : 150,hide : 'true'},
		        {display : 'user.zhanghao',name : 'account',width : _columnWidth*1.18},
                // {display : 'user.yuangongbianhao',name : 'employeeCode',width : _columnWidth},
				{display : 'user.yonghuming',name : 'name',width : _columnWidth}, 
				{display : 'user.suoding',name : 'locked',width : 100, process:function(value){
				 	return value=='0' ? getMessage("common.no") : getMessage("common.yes");
				}},
				{display : 'user.dianhua',name : 'telephone',width : _columnWidth}, 
				{display : 'user.youxiang',name : 'email',width : _columnWidth},
				{display:'common.operation',name:"id", width : 200, process:function(value,index){
					var html = [];
					html.push('<button iss-permission="B_USER_UPDATE" class="btn m-r-xs btn-xs btn-primary" id="updateBtn" ng-click="vm.update('+ value +')">');
					html.push('<i class="fa fa-pencil"></i>');
					html.push('<span translate="common.edit"></span>');
					html.push('</button>');
					html.push('<button iss-permission="B_USER_DELETE" class="btn m-r-xs btn-xs btn-danger" id="delBtn" ng-click="vm.del('+ index +')">');
					html.push('<i class="fa fa-trash-o"></i>');
					html.push('<span translate="common.del"></span>');
					html.push('</button>');
					html.push('<button iss-permission="B_USER_RESETPWD" class="btn m-r-xs btn-xs btn-primary" id="resetPwd" ng-click="vm.resetPwd('+ index +')">');
					html.push('<i class="fa fa-puzzle-piece"></i>');
					html.push('<span translate="user.mimachongzhi"></span>');
					html.push('</button>');		
				 	return html.join('');
				}}
	        ],
			usepager: true,
	        showcheckbox: true,
	        rp: 10,
			isDragCol: true
		};
	}
	this.add = function(data){
		return $http.post(
				ISS.getPath('/user/add'),
				data,
				{contentType:'json'}
			).then(function(resp){
				return resp.data;
			});
	}
	this.initUpdate = function(id){
		return $http.post(
				ISS.getPath('/user/get/'+ id)
			).then(function(resp){
				return resp.data;
			});
	}
	this.update = function(data){
		delete data.createTime;
		return $http.post(
				ISS.getPath('/user/update'),
				data,
				{contentType:'json'}
			).then(function(resp){
				return resp.data;
			});
	}
	this.remove = function(data){
		return $http.post(
				ISS.getPath('/user/delete/'+ data.id + '/' + data.account)
				).then(function(resp){
					return resp.data;
				});
	}
	//解锁
	this.unlock = function(ids, accounts){
		return $http.post(
					ISS.getPath('/user/unlock'),
					{'userIds':ids, 'accounts': accounts}
				).then(function(resp){
					return resp.data;
				});
	}
	//重置密码
	this.resetPwd = function(data){
		var dlg = dialogs.create(
				ISS.getResPath('tpl/blocks/setting/user/reset-pwd.html'), 
				'UserResetPwdCtrl', 
				{id:data.id, account:data.account}
			);
		return dlg.result.then(function(newData){
			return newData;
		});
	}
	this.resetPwd1 = function(data){
		return $http.post(
				ISS.getPath('/user/resetPwd'),
					data,
					{contentType: 'json'}
				).then(function(resp){
					return resp.data;
				});
	}
	this.getRoles = function(ids){
		return $http.post(
					ISS.getPath('/user/treeUser'),
					{userId: ids}
				).then(function(resp){
					var list = resp.data;
					var _list = [];
					angular.forEach(list, function(d){
						if(d.id != 0){
							_list.push(d);
						}
					});
					return _list;
				});	
	}
	//得到所有新增用户时需要用到的数据
	this.getInitData = function(ids){
		var data = {};
		var deferred = $q.defer();
		that.getRoles(ids).then(function(roleList){
			data.roleList = roleList;
			deferred.resolve(data);
		});
		return deferred.promise;
	}
}]);